//
//  CQBadgeBtn.m
//  01-OC微博
//
//  Created by 陈乾 on 15/10/25.
//  Copyright © 2015年 陈乾. All rights reserved.
//

#import "CQBadgeBtn.h"
#define CQFont [UIFont systemFontOfSize:11]
@implementation CQBadgeBtn

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        //设置背景
        [self setBackgroundImage:[UIImage imageNamed:@"main_badge"] forState:UIControlStateNormal];
        //设置字体的大小
        self.titleLabel.font = CQFont;
        //设置frame
        [self sizeToFit];
        //设置不可交互
        self.userInteractionEnabled = NO;
    }
    return self;
}

-(void)setBadgeValue:(NSString *)badgeValue
{
    _badgeValue = badgeValue;
    //如果有值就显示
    if (badgeValue.length == 0 || [badgeValue isEqualToString:@"0"])
    {
        self.hidden = YES;
    }
    else
    {
        self.hidden = NO;
    }
    CGSize size = [badgeValue sizeWithFont:CQFont];
    //如果 文字比按钮宽
    if (size.width > self.frame.size.width) {
        [self setBackgroundImage:nil forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:@"new_dot"] forState:UIControlStateNormal];
        [self setTitle:nil forState:UIControlStateNormal];
        
    }
    else
    {
        [self setBackgroundImage:[UIImage imageNamed:@"main_badge"  ]forState:UIControlStateNormal];
        [self setImage:nil forState:UIControlStateNormal];
        [self setTitle:badgeValue forState:UIControlStateNormal];
      
    }

    
    
}

@end
